package com.tbit.mvc.convert;


import com.tbit.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.util.Date;

/**
 * @author Kang
 * @date 2018年1月16日下午6:54:15
 * @description 全局日期转化处理
 */
@Slf4j
public class StringToDateConvert implements Converter<String, Date> {

    private final int leng_yyyyMMdd = 10;
    private final int leng_yyyyMMddHHmmss = 19;

    @Override
    public Date convert(String source) {
        try {
            if (source.length() == leng_yyyyMMdd) {

                return DateUtil.YMD_.parse(source);
            } else if (source.length() == leng_yyyyMMddHHmmss) {
                return DateUtil.YMDHMS_.parse(source);
            }
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }
}
